package com.piece.core.resource.support;

import com.piece.core.web.util.AuthenticationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.authentication.TokenExtractor;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
import javax.servlet.http.HttpServletRequest;

@Slf4j
public class CustomTokenExtractor implements TokenExtractor {

    public CustomTokenExtractor() {
    }

    @Override
    public Authentication extract(HttpServletRequest request) {
        String tokenValue = AuthenticationUtil.getInstance(request).extractToken();
        if (null != tokenValue) {
            PreAuthenticatedAuthenticationToken authentication = new PreAuthenticatedAuthenticationToken(tokenValue, "");
            return authentication;
        } else {
            return null;
        }
    }
}
